
import nodePkg from '../../../topo/connectnode.js'
import absorbpossvc from '../../../service/absorbpossvc.js'
import UtilFuncs from '../../../common/tools/utilfuncs.js'
var funcs = UtilFuncs.UtilFuncs;
import { LGXEnums } from '../../../common/sysenum.js';

import globalConst from '../../../common/globalConst.js';
var global = globalConst.GlobalConst;

var PlotSvc = {}

PlotSvc.connect2DstGraph = function (topoManager, mousepoint, pCurentGraph, pDstGraph, isFirstNew, scaling) {
    if (!pCurentGraph || !pDstGraph) {
        return;
    }
    if (pCurentGraph.m_graphType != LGXEnums.LGXGraphType.LineGraph_type
        && pDstGraph.m_graphType != LGXEnums.LGXGraphType.LineGraph_type) {
        return;
    }

    let lineGraph = null;
    let anotherGraph = null;
    if (pCurentGraph.m_graphType == LGXEnums.LGXGraphType.LineGraph_type
        && pDstGraph.m_graphType != LGXEnums.LGXGraphType.LineGraph_type) {
        lineGraph = pCurentGraph;
        anotherGraph = pDstGraph;
    }
    else if (pCurentGraph.m_graphType != LGXEnums.LGXGraphType.LineGraph_type
        && pDstGraph.m_graphType == LGXEnums.LGXGraphType.LineGraph_type) {
        lineGraph = pDstGraph;
        anotherGraph = pCurentGraph;
    }
    if (!lineGraph || !anotherGraph) {
        return;
    }
    if (lineGraph.getType() == global.Polyline_noneTopo
        || lineGraph.getType() == global.TwoPtline_noneTopo) {
        //纯多段线，无拓扑关系
        return;
    }
    var curentRes = pCurentGraph.getTopoRes();
    var destRes = pDstGraph.getTopoRes();
    if (curentRes && destRes) {
        let isNewNode = false;
        var connNode = this.getTwoGraphConnNode(lineGraph, anotherGraph, mousepoint);
        if (!connNode) {
            let lineRes = lineGraph.getTopoRes();
            let devRes = anotherGraph.getTopoRes();
            connNode = topoManager.createNewNode();
            connNode.id = funcs.generateUUID();
            lineRes.addNode4Edit(connNode, mousepoint);
            devRes.addNode(connNode);
            isNewNode = true;
        }
        let pos = this.calConnPos(mousepoint, lineGraph, anotherGraph, isNewNode, scaling);
        if (pos) {
            connNode.setPos(pos.x, pos.y);
        }
    }
}

//检测两个图形间在当前端点上是否有共同的连接点，如果有，表示已经连接，此时只要修正连接点坐标即可
PlotSvc.getTwoGraphConnNode = function (lineGraph, anotherGraph, mousepoint) {
    let lineRes = lineGraph.getTopoRes();
    let anotherRes = anotherGraph.getTopoRes();

    //本次变动将要连接的线路端点对应的节点对象
    let curentMayConnNode = null;
    let position = lineGraph.getPosCopy();
    let headPt = position.getHead();
    let tailPt = position.getTail();
    let dis1 = funcs.distancePoint(headPt, mousepoint, 1);
    let dis2 = funcs.distancePoint(tailPt, mousepoint, 1);
    if (dis1 < dis2) {
        curentMayConnNode = lineRes.stNode;
    }
    else {
        curentMayConnNode = lineRes.endNode;
    }

    // let lineNodeList = lineRes.m_connNodeList;
    // let anotherNodeList = anotherRes.m_connNodeList;
    // let connSameNode = null;
    // for (let i = 0; i < lineNodeList.length; i++) {
    //     let node1 = lineNodeList[i];
    //     for (let n = 0; n < anotherNodeList.length; n++) {
    //         let node2 = anotherNodeList[n];
    //         if (node1 == node2) {
    //             connSameNode = node1;
    //             break;
    //         }
    //     }
    //     if (connSameNode) {
    //         break;
    //     }
    // }
    // if (curentMayConnNode == connSameNode){
    //     connSameNode = null;
    // }
    return curentMayConnNode;
}

PlotSvc.calConnPos = function (mousepoint, pLineGraph, pDstGraph, isNewNode, scaling) {
    if (!pLineGraph || !pDstGraph || !isNewNode) {
        return;
    }

    var linePos = pLineGraph.getPos();
    var posOnSymbol = this.calConnPosOnSymbolGraph(mousepoint, pDstGraph, scaling);
    if (posOnSymbol) {
        let p1 = linePos.getHead();
        let p2 = linePos.getTail();
        let dis1 = funcs.distancePoint(mousepoint, p1, 1);
        let dis2 = funcs.distancePoint(mousepoint, p2, 1);
        let lnEndPt = p1;
        if (dis2 < dis1) {
            lnEndPt = p2;
        }
        lnEndPt.x = posOnSymbol.x;
        lnEndPt.y = posOnSymbol.y;
    }
    return posOnSymbol;
}

PlotSvc.calConnPosOnSymbolGraph = function (mousepoint, pDstGraph, scaling) {
    if (!pDstGraph) {
        return;
    }
    var symbol = pDstGraph.getSymbol();
    var posOnConnGraph = null;
    if (symbol) {
        var dstGraphPos = pDstGraph.getPosCopy();
        posOnConnGraph = this.getPosOnSymbol(mousepoint, dstGraphPos, symbol, scaling);
    }
    else {
        posOnConnGraph = this.getPosOnBaseGraph(mousepoint, pDstGraph, scaling);
    }
    return posOnConnGraph;
}

PlotSvc.getPosOnSymbol = function (mousePt, devPos, symbol, scaling) {
    var mindis = 0;
    var index = 0;
    var x = mousePt.x;
    var y = mousePt.y;
    var absorbDstPt = { x, y };
    var unitGraphLst = symbol.getGraphUnitLst();
    for (var idx in unitGraphLst) {
        var graph = unitGraphLst[idx];
        var unitGraphPos = graph.getPos();
        var tmpUnitPos = unitGraphPos.createCopyPos(devPos);
        var tmpPt = absorbpossvc.AbsorbPosSvc.calculateOnGraph(graph, devPos, mousePt, scaling);
        if (tmpPt) {
            var dis = funcs.distancePoint(tmpPt, mousePt, 1);
            if (index == 0 || mindis > dis) {
                mindis = dis;
                absorbDstPt.x = tmpPt.x;
                absorbDstPt.y = tmpPt.y;
            }
            index++;
        }
    }
    return absorbDstPt;
}

PlotSvc.getPosOnBaseGraph = function (mousePt, pDstGraph, scaling) {
    var x = mousePt.x;
    var y = mousePt.y;
    var absorbDstPt = { x, y };
    var tmpPt = absorbpossvc.AbsorbPosSvc.calculateOnGraph(pDstGraph, { x: 0, y: 0 }, mousePt, scaling);
    if (tmpPt) {
        absorbDstPt.x = tmpPt.x;
        absorbDstPt.y = tmpPt.y;
    }
    return absorbDstPt;
}
export default { PlotSvc }